package Xml;

import java.util.ArrayList;
import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * XmlTest
 */
public class XmlTest {

    /**
     * XML转对象
     * 
     * @param clazz 对象类
     * @param str   xml字符串
     * @param <T>   T
     * @return
     */
    public static <T> T deserializeXml(Class<T> clazz, String xml) {
        // 创建解析XML对象
        XStream xStream = new XStream(new DomDriver());
        // 处理注解
        xStream.processAnnotations(clazz);
        // xStream.autodetectAnnotations(true);// 自动注解扫描，否则注解不生效，默认不开启
        @SuppressWarnings("unchecked")
        // 将XML字符串转为bean对象
        T t = (T) xStream.fromXML(xml);
        return t;
    }

    /**
     * 对象转xml
     * 
     * @param obj 对象
     * @return
     */
    public static String serializeXml(Object obj) {
        XStream xStream = new XStream(new DomDriver());
        xStream.processAnnotations(obj.getClass());
        return xStream.toXML(obj);
    }

    public static void main(String[] args) {
        User user = new User();
        user.setId(1);
        user.setName("张三");
        user.setAge(26);
        user.setType(1);

        List<Course> course = new ArrayList<Course>();
        course.add(new Course("英文"));
        course.add(new Course("数学"));
        user.setCourse(course);

        // 创建解析XML对象
        XStream xStream = new XStream();
        xStream.autodetectAnnotations(true);// 自动注解扫描，否则注解不生效，默认不开启
        // xStream.alias("user", User.class);//手动设置别名, 默认会输出全路径

        /**
         * 序列化
         */
        String xml = xStream.toXML(user);// 转为xml
        System.out.println("xml内容：");
        System.out.println(xml);
        System.out.println(serializeXml(user));
        /**
         * 反序列化
         */
        XStream xstream1 = new XStream(new DomDriver()); // 注意：不是new Xstream(); 否则报错：java.lang.NoClassDefFoundError:
                                                         // org/xmlpull/v1/XmlPullParserFactory
        xstream1.processAnnotations(User.class); // 如果是用注解的方式，这句不能少
        User user2 = (User) xstream1.fromXML(xml);
        user2 = deserializeXml(User.class, xml);
        System.out.print("Name:" + user2.getName() + ",Age:" + user2.getAge() + ",Id:" + user2.getId());
    }

}

@XStreamAlias("user") // 定义根节点别名，等同于xStream.alias("user", User.class);
class User {

    @XStreamAlias("Id") // 定义属性别名
    private int id;

    @XStreamAlias("Name") // 定义属性别名
    private String name;

    @XStreamOmitField // 忽略该属性
    private int age;

    @XStreamAsAttribute // 将type作为User属性输出在父节点
    private int type;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public int getType() {
        return age;
    }

    public void setType(int type) {
        this.type = type;
    }

    // @XStreamImplicit //集合常用的注解 @XStreamImplicit 去除<courses></courses>显示
    @XStreamAlias("Courses")
    private List<Course> courses;

    public List<Course> getCourse() {
        return courses;
    }

    public void setCourse(List<Course> courses) {
        this.courses = courses;
    }
}

@XStreamAlias("Course")
class Course {
    public Course(String name) {
        this.CName = name;
    }

    private String CName;
}
